package com.cytech.cloudclinic.moudle.main.drug.add.dosage;

import android.text.TextUtils;

import com.cytech.cloudclinic.base.BaseBean;
import com.cytech.cloudclinic.base.BaseObserver;
import com.cytech.cloudclinic.base.BasePresenter;
import com.cytech.cloudclinic.bean.AddDrugTag;
import com.cytech.cloudclinic.bean.req.AddDrugReq;
import com.cytech.cloudclinic.bean.req.BasePage;
import com.cytech.cloudclinic.bean.resp.RespDrugDetail;
import com.cytech.cloudclinic.bean.resp.RespPatientList;
import com.cytech.cloudclinic.moudle.main.drug.add.base.AddDrugBaseView;
import com.yechaoa.yutils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class AddDrugDosagePresenter extends BasePresenter<AddDrugDosageView>{
    public AddDrugDosagePresenter(AddDrugDosageView baseView) {
        super(baseView);
    }

    /**
     * 获取药品标签列表
     */
    public void getSummaryList(int modeId,int drugId) {
        JSONObject json = new JSONObject();
        try {
            json.put("mode_id",modeId);
            json.put("drug_id",drugId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        addDisposable(apiServer.getSummaryList(body), new BaseObserver<BaseBean<List<AddDrugTag>>>(baseView, false) {

            @Override
            public void onSuccess(BaseBean<List<AddDrugTag>> o) {
                switch (modeId){
                    case 1:
                        baseView.getMinPackageSuccess(o.result);
                        break;
                    case 2:
                        baseView.getFrequencySuccess(o.result);
                        break;
                    case 3:
                        baseView.getUseSuccess(o.result);
                        break;
                    case 4:
                        baseView.getPackageSuccess(o.result);
                        break;
                }
            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }

    /**
     * 添加药品标签列表
     */
    public void addSummaryTag(int modeId,int drugId,String value) {
        JSONObject json = new JSONObject();
        try {
            json.put("mode",modeId);
            json.put("drug_tag",drugId);
            json.put("value",value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        addDisposable(apiServer.addSummaryTag(body), new BaseObserver<BaseBean<Object>>(baseView, true) {

            @Override
            public void onSuccess(BaseBean<Object> o) {

            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }

    public List<AddDrugTag> buildNumValue(){
        List<AddDrugTag> numList = new ArrayList<>();
        numList.add(new AddDrugTag("1",false));
        numList.add(new AddDrugTag("2",false));
        numList.add(new AddDrugTag("3",false));
        numList.add(new AddDrugTag("4",false));
        numList.add(new AddDrugTag("5",false));
        numList.add(new AddDrugTag("6",false));
        numList.add(new AddDrugTag("7",false));
        numList.add(new AddDrugTag("8",false));
        numList.add(new AddDrugTag("9",false));
        numList.add(new AddDrugTag("0",false));
        numList.add(new AddDrugTag(".",false));
        numList.add(new AddDrugTag("清除",false));
        return numList;
    }

    public String getRealNumValue(String value,String etValue){
        if(value.equals("清除")){
            return "";
        }else if(value.equals("0")){
            if(TextUtils.isEmpty(etValue)){
                return "0";
            }else{
                etValue = etValue+0;
                return etValue;
            }
        }else if(value.equals(".")){
            if(TextUtils.isEmpty(etValue)){
                return "0.";
            }else{
                return etValue+".";
            }
        }else{
            //第一位数位0的情况，直接把0去掉
            if(etValue.equals("0")){
                return value;
            }else{
                return etValue+value;
            }
        }
    }


    /**
     * 添加药品
     */
    public void addDrug(AddDrugReq drugReq){
        addDisposable(apiServer.addDrug(objectToRequestBody(drugReq)), new BaseObserver<BaseBean<RespPatientList>>(baseView, true) {

            @Override
            public void onSuccess(BaseBean<RespPatientList> o) {
                baseView.addDrugSuccess();
            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }

    /**
     * 删除药品标签
     */
    public void delSummaryTag(int modeId) {
        JSONObject json = new JSONObject();
        try {
            json.put("id",modeId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        addDisposable(apiServer.delSummaryTag(body), new BaseObserver<BaseBean<Object>>(baseView, true) {

            @Override
            public void onSuccess(BaseBean<Object> o) {
                ToastUtil.showToast("删除成功");
            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }

    /**
     * 获取药品详情
     */
    public void getDrugDetail(int drugId) {
        JSONObject json = new JSONObject();
        try {
            json.put("drug_id",drugId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        addDisposable(apiServer.detailDrug(body), new BaseObserver<BaseBean<RespDrugDetail>>(baseView, false) {

            @Override
            public void onSuccess(BaseBean<RespDrugDetail> o) {
                baseView.getDrugDetailSuccess(o.result);
            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }
}
